
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Bring in the expenditure tercile estimates
load(file.path(root, "tmp/synth_expTercilesEB.RData"))

# Set up the matrices for storing output
out = matrix(nrow=6, ncol=6)

# Name the rows and columns of the output matrices
estimators = c("vs_small", "vs_mid", "vs_large", 
  "to_small", "to_mid", "to_large")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(out) = estimators
rownames(out) = output_components

# Store the estimated effects and standard errors
out[1,] = c(t1_synth_dem_sdideb$est,
  t2_synth_dem_sdideb$est,
  t3_synth_dem_sdideb$est,
  t1_synth_tur_sdideb$est,
  t2_synth_tur_sdideb$est,
  t3_synth_tur_sdideb$est)
out[2,] = c(sd(t1_synth_dem_sdideb$est_boot),
  sd(t2_synth_dem_sdideb$est_boot),
  sd(t3_synth_dem_sdideb$est_boot),
  sd(t1_synth_tur_sdideb$est_boot),
  sd(t2_synth_tur_sdideb$est_boot),
  sd(t3_synth_tur_sdideb$est_boot))

# Count the number of years and units
get_sdideb_n = function(x){
  n_recipients = round(sum(x$omega))
  n_counties = length(x$omega) + n_recipients
  n = length(x$lambda)*n_counties
  return(c(n_recipients, n_counties, n))
}
out[4:6, ] = cbind(get_sdideb_n(t1_synth_dem_sdideb), 
  get_sdideb_n(t2_synth_dem_sdideb), get_sdideb_n(t3_synth_dem_sdideb), 
  get_sdideb_n(t1_synth_tur_sdideb), get_sdideb_n(t2_synth_tur_sdideb), 
  get_sdideb_n(t3_synth_tur_sdideb))

# Put the estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in 2020, Grant Size Tercile, Entropy Balancing Weights.}",
"\\label{tab:tercile_ebal}}",
"\\begin{tabular}{lccc|ccc}",
"\\toprule \\toprule",
" & \\multicolumn{3}{c}{Dem Vote Share (\\%)} & \\multicolumn{3}{c}{Turnout (\\%)} \\\\",
" & Small & Medium & Large & Small & Medium & Large \\\\",
" & Grant & Grant & Grant & Grant & Grant & Grant \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(out[1,]), " \\\\"),
paste0(prep_table_rows(out[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(out[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(out[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(out[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"County Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{7}{p{0.9\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Grant sizes are determined by tercile of grant size per voting age resident among recipients. Small is the smallest tercile, and large is the largest tercile.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/expterc_effects_table_ebal.tex"), sep="\n")

